#include "Engine/inc/SGEngine.h"

#pragma comment(lib,"Winmm.lib")


SGEngine::SGEngine()
: m_Renderer(NULL)
{
	PreInitialize();
}

SGEngine::~SGEngine()
{
	delete m_Renderer;
}

BOOL SGEngine::Start()
{
	return TRUE;
}

BOOL SGEngine::End()
{
	return TRUE;
}

BOOL SGEngine::Update(FLOAT DeltaSeconds)
{
	m_Renderer->Tick(DeltaSeconds);

	return TRUE;
}


BOOL SGEngine::Initialize(HINSTANCE hInstance)
{
	if (InitializeRenderer(hInstance) == FALSE)
		return FALSE;

	return TRUE;
}

BOOL SGEngine::Loop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (msg.message != WM_QUIT)
	{
		if (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}
		else
		{
			FLOAT fCurrentTime = (FLOAT)timeGetTime();
			
			Update(fCurrentTime - m_LastTickTime);
		}
	}

	return TRUE;
}

void SGEngine::PreInitialize()
{

}

BOOL SGEngine::InitializeRenderer(HINSTANCE hInstance)
{
	m_Renderer = new SGRenderer();
	if (m_Renderer == NULL)
		return FALSE;

	return m_Renderer->Initialize(hInstance);
}

BOOL SGEngine::Tick(FLOAT fDeltaSeconds)
{
	return TRUE;
}

LRESULT CALLBACK SGEngine::WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
	switch (iMessage)
	{
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;
	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE)
			::DestroyWindow(hWnd);
		break;
	}

	return DefWindowProc(hWnd, iMessage, wParam, lParam);
}
